function fig = fm_matrx()
% FM_MATRX create GUI for matrix visualizations
%
% HDL = FM_MATRX()
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Version:   1.0.0
%
%E-mail:    Federico.Milano@uclm.es
%Web-site:  http://www.uclm.es/area/gsee/Web/Federico
%
% Copyright (C) 2002-2013 Federico Milano

global Settings Theme Path Fig File

% check for data file
if isempty(File.data)
  fm_disp('Set a data file for running sparse matrix visualization.',2)
  return
end
% check for initial power flow solution
if ~Settings.init
  fm_disp('Solve base case power flow...')
  Settings.show = 0;
  fm_set lf
  Settings.show = 1;
  if ~Settings.init, return, end
end
if Fig.matrx, figure(Fig.matrx), return, end

mappe = {'hsv';'hot';'gray';'bone';'copper';'pink';'white';'flag';
  'lines';'colorcube';'vga';'jet';'prism';'cool';
  'autumn';'spring';'winter';'summer'};

%screensize = get(0,'ScreenSize');

h0 = figure('Color',Theme.color01, ...
  'Units', 'normalized', ...
  'CreateFcn','Fig.matrx = gcf;', ...
  'DeleteFcn','Fig.matrx = 0; rotate3d off', ...
  'FileName','fm_matrx', ...
  'MenuBar','none', ...
  'Name','Matrix Visualization', ...
  'NumberTitle','off', ...
  'PaperPosition',[18 180 576 432], ...
  'PaperUnits','points', ...
  'Position',sizefig(0.6,0.6), ...
  'Resize','on', ...
  'ToolBar','none');
fm_set colormap
h1 = axes('Parent',h0, ...
  'Box','on', ...
  'CameraUpVector',[0 1 0], ...
  'CameraUpVectorMode','manual', ...
  'Color',Theme.color11, ...
  'ColorOrder',Settings.color, ...
  'Position',[0.0765 0.2550 0.5897 0.6805], ...
  'Tag','Axes1', ...
  'XColor',[0 0 0], ...
  'YColor',[0 0 0], ...
  'ZColor',[0 0 0]);
set(h0,'UserData',h1);
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'ForegroundColor',Theme.color03, ...
  'Position',[0.73219     0.49761     0.20317      0.4386], ...
  'Style','frame', ...
  'Tag','Frame1');
h1 = uicontrol('Parent',h0, ...
  'BackgroundColor',Theme.color02, ...
  'Units', 'normalized', ...
  'Callback','fm_view(5);', ...
  'Position',[0.76121      0.5327     0.14512    0.052632], ...
  'String','Jlfd Matrix', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'BackgroundColor',Theme.color02, ...
  'Units', 'normalized', ...
  'Callback','fm_view(4);', ...
  'Position',[0.76121     0.61085     0.14512    0.052632], ...
  'String','Jlfv Matrix', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'BackgroundColor',Theme.color02, ...
  'Units', 'normalized', ...
  'Callback','fm_view(3);', ...
  'Position',[0.76121       0.689     0.14512    0.052632], ...
  'String','Jlf Matrix', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'BackgroundColor',Theme.color02, ...
  'Units', 'normalized', ...
  'Callback','fm_view(2);', ...
  'Position',[0.76121     0.76874     0.14512    0.052632], ...
  'String','State Matrix', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'BackgroundColor',Theme.color02, ...
  'Units', 'normalized', ...
  'Callback','fm_view(1);', ...
  'Position',[0.76121     0.84689     0.14512    0.052632], ...
  'String','Complete Matrix', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'ForegroundColor',Theme.color03, ...
  'Position',[0.33509    0.063796     0.32718    0.089314], ...
  'Style','frame', ...
  'Tag','Frame2');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_arrowr'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback',' brighten(0.1);', ...
  'Position',[0.6075   0.0835   0.0375   0.05], ...
  'TooltipString','Brighten color map', ...
  'Tag','Pushbutton2');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_arrowl'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback',' brighten(-0.1);', ...
  'Position',[0.57   0.0835   0.0375   0.05], ...
  'TooltipString','Darken color map', ...
  'Tag','Pushbutton2');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'Callback','String = get(gcbo,''String''); eval([''colormap '',String{get(gcbo,''Value'')}]);', ...
  'BackgroundColor',Theme.color04, ...
  'Position',[0.355    0.0835     0.20712    0.05], ...
  'String',mappe, ...
  'Style','popupmenu', ...
  'Tag','PopupMenu1', ...
  'Value',1);

% Frame and push buttons for selecting the graph type
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'ForegroundColor',Theme.color03, ...
  'Position',[0.7322  0.1914  0.2032   0.2536], ...
  'Style','frame', ...
  'Tag','Frame3');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_surface'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_view(6)', ...
  'Position',[0.73219+0.0116     0.1914+0.0068+0.12      0.0900  0.1200], ...
  'TooltipString','Surface visualization', ...
  'Style','togglebutton', ...
  'Tag','toggle1', ...
  'Value',1);
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_mesh'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_view(6)', ...
  'Position',[0.73219+0.0116     0.1914+0.0068  0.0900  0.1200], ...
  'TooltipString','Mesh visualization', ...
  'Style','togglebutton', ...
  'Tag','toggle2');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_sparse'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_view(6)', ...
  'Position',[0.73219+0.0116+0.09     0.1914+0.0068+0.12  0.0900  0.1200], ...
  'TooltipString','Sparsity visualization', ...
  'Style','togglebutton', ...
  'Tag','toggle3');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_filpol'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_view(6)', ...
  'Position',[0.73219+0.0116+0.09   0.1914+0.0068  0.0900  0.1200], ...
  'TooltipString','Filled Polygon', ...
  'Style','togglebutton', ...
  'Tag','toggle4');

% Frame and push buttons for axis manipulation
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'ForegroundColor',Theme.color03, ...
  'Position',[0.0726  0.0638  0.2000  0.0893], ...
  'Style','frame', ...
  'Tag','Frame2');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_rotate'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','rotate3d(Fig.matrx), , if(get(gcbo,''Value'')), set(findobj(gcf,''Tag'',''toggle7''),''Value'',0), zoom(Fig.matrx,''off''), end', ...
  'Position',[0.0776  0.0685  0.0600  0.0800], ...
  'TooltipString','Rotate graph', ...
  'Style','togglebutton', ...
  'Tag','toggle5');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_grid'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','grid(get(Fig.matrx,''UserData''))', ...
  'Position',[0.1426  0.0685  0.0600  0.0800], ...
  'TooltipString','Grid', ...
  'Style','togglebutton', ...
  'Tag','toggle6');
h1 = uicontrol('Parent',h0, ...
  'CData',fm_mat('mat_zoomxy'), ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','zoom, if(get(gcbo,''Value'')), set(findobj(gcf,''Tag'',''toggle5''),''Value'',0), rotate3d(Fig.matrx,''off''), end', ...
  'Position',[0.2076  0.0685  0.0600  0.0800], ...
  'TooltipString','Zoom', ...
  'Style','togglebutton', ...
  'Tag','toggle7');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'Callback','close(gcf);', ...
  'FontWeight','bold', ...
  'ForegroundColor',Theme.color09, ...
  'Position',[0.73087    0.065391     0.20317    0.089314], ...
  'String','Close', ...
  'Tag','Pushbutton3');

if nargout > 0, fig = h0; end